//
//  SLPackageOrBoatTypeViewController.m
//  StarLight
//
//  Created by 吴涛 on 16/5/29.
//  Copyright © 2016年 Duke.Wu. All rights reserved.
//

#import "SLPackageOrBoatTypeViewController.h"
#import "STPopup.h"
#import "SLCenterLabelTableViewCell.h"


@interface SLPackageOrBoatTypeViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) NSArray *packageSource;

@property (nonatomic, strong) NSArray *boatStateSource;

@property (nonatomic, strong) NSArray *boatTypeSource;

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, assign)PackageOrBoatType type;

@end

@implementation SLPackageOrBoatTypeViewController

- (instancetype)initWithType:(PackageOrBoatType)type{
  if (self = [super init]) {
    
    self.contentSizeInPopup = CGSizeMake(SCREEN_WIDTH-80, 200*MIX_H);
    self.landscapeContentSizeInPopup = CGSizeMake(400*MIX_W, 200*MIX_H);
    [STPopupNavigationBar appearance].barTintColor = [UIColor whiteColor];
    [STPopupNavigationBar appearance].tintColor = [UIColor whiteColor];
    [STPopupNavigationBar appearance].barStyle = UIBarStyleDefault;
    [STPopupNavigationBar appearance].titleTextAttributes = @{ NSForegroundColorAttributeName: SLYellowColor };
    self.type = type;
    if (type == PackageOrBoatTypeBoat) {
      self.title = @"船舶种类";
    }else if(type == PackageOrBoatTypePackage){
      self.title = @"包装形式";
    }
  }
  return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
  [self.view addSubview:self.tableView];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated{
  [super viewWillAppear:animated];
  [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (void)returnReturnPackageOrBoatType:(ReturnPackageOrBoatType)block{
  self.backBlock = block;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView willDisplayCell:(SLCenterLabelTableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
  if (self.type == PackageOrBoatTypePackage) {
    cell.centerLabel.text = self.packageSource[indexPath.row];
  }else if(self.type == PackageOrBoatTypeBoat){
    cell.centerLabel.text = self.boatTypeSource[indexPath.row];
  }else{
    cell.centerLabel.text = self.boatStateSource[indexPath.row];
  }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  return 40;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  if (self.type == PackageOrBoatTypePackage) {
    self.backBlock(self.packageSource[indexPath.row]);
  }else if(self.type == PackageOrBoatTypeBoat){
    self.backBlock(self.boatTypeSource[indexPath.row]);
  }else{
    self.backBlock([NSString stringWithFormat:@"%ld",(long)indexPath.row]);
  }
  
  [self dismissViewControllerAnimated:YES completion:nil];
  
}


#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  if (self.type == PackageOrBoatTypePackage) {
    return self.packageSource.count;
  }else if(self.type == PackageOrBoatTypeBoat){
    return self.boatTypeSource.count;
  }else{
    return self.boatStateSource.count;
  }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  
  SLCenterLabelTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
  
  return cell;
  
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (UITableView *)tableView{
  if (!_tableView) {
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [_tableView registerClass:[SLCenterLabelTableViewCell class] forCellReuseIdentifier:@"cell"];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableFooterView = [UIView new];
  }
  return _tableView;
}


- (NSArray *)packageSource{
  if (!_packageSource) {
    _packageSource = @[@"散装",@"袋装",@"箱装",@"罐桶装",@"其他"];
  }
  return _packageSource;
}

- (NSArray *)boatTypeSource{
  if (!_boatTypeSource) {
    _boatTypeSource = @[@"杂货船",@"油船",@"散装水泥船",@"冷藏船",@"集装箱船",@"散货船",@"拖队",@"化学品船",@"液化气船",@"滚装船",@"多用途船",@"捕捞船",@"其他船",];
  }
  return _boatTypeSource;
}

- (NSArray *)boatStateSource{
  if(!_boatStateSource){
    _boatStateSource = @[@"满载",@"空船找货",@"半船配货",@"预定货物"];
  }
  return _boatStateSource;
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
